/*
 * Observer.cpp
 *
 *  Created on: 20 déc. 2011
 *      Author: M. Alexandre (malexand314@gmail.com)
 */

#include "Observer.h"

namespace Framework
{

    ObserverSubject::ObserverSubject()
    {
        pthread_mutex_init(&m_observerMutex, NULL);
    }

    ObserverSubject::~ObserverSubject()
    {
        pthread_mutex_destroy(&m_observerMutex);
    }

    void ObserverSubject::attachObserver(Observer* _ptObserver)
    {
        pthread_mutex_lock(&m_observerMutex);
        m_listObserver.push_back(_ptObserver);
        pthread_mutex_unlock(&m_observerMutex);
    }

    void ObserverSubject::detachObserver(Observer* _ptObserver)
    {
        pthread_mutex_lock(&m_observerMutex);
        m_listObserver.remove(_ptObserver);
        pthread_mutex_unlock(&m_observerMutex);
    }

    void ObserverSubject::notify()
    {
        pthread_mutex_lock(&m_observerMutex);
        for(std::list<Observer*>::iterator it = m_listObserver.begin(); it != m_listObserver.end(); it++)
        {
            (*it)->update(this);
        }
        pthread_mutex_unlock(&m_observerMutex);
    }

}
